package com.youi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 日期工具
 * 
 * @author erdong.Chan
 *
 */
public class DateUtil {
	
	public static SimpleDateFormat simpdfMdyHm = new SimpleDateFormat("MM/dd/yyyy HH:mm");
	
	public static SimpleDateFormat simpdfyMdHms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static SimpleDateFormat simpdfyMdHm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	public static SimpleDateFormat simpdfyMdH = new SimpleDateFormat("yyyy-MM-dd HH");
	
	public static SimpleDateFormat simpdfHms = new SimpleDateFormat("HH:mm:ss");
	
	public static SimpleDateFormat simpdfHm = new SimpleDateFormat("HH:mm");

	public static SimpleDateFormat simpdfH = new SimpleDateFormat("HH");
	
	public static SimpleDateFormat simpdfyMd = new SimpleDateFormat("yyyy-MM-dd");

	public static SimpleDateFormat simpdfMd = new SimpleDateFormat("MM-dd");
	
	public static SimpleDateFormat simpdfd = new SimpleDateFormat("dd");

	public static SimpleDateFormat simpdfyMdTow = new SimpleDateFormat("yyyy/MM/dd");

	public static SimpleDateFormat simpdfyM = new SimpleDateFormat("yyyy-MM");

	public static SimpleDateFormat simpdfy = new SimpleDateFormat("yyyy");

	public static SimpleDateFormat simpyMdHHmmss=new SimpleDateFormat("yyyyMMddHHmmss");
	
	public static SimpleDateFormat simpyMd=new SimpleDateFormat("yyyyMMdd");

	/**
	 * 时间增减操作
	 * 
	 * @param date
	 * @param field
	 * @param amount
	 * @return
	 */
	public static Date getCalendarByAdd(Date date, int field, Integer amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	/**
	 * 查询某日期星期的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getWeekFirstDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int len = calendar.get(Calendar.DAY_OF_WEEK);
		return getCalendarByAdd(date, Calendar.DAY_OF_MONTH, len * -1 + 1);
	}

	/**
	 * 查询某日期星期的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getWeekLastDay(Date date) {
		return getCalendarByAdd(getWeekFirstDay(date), Calendar.DAY_OF_MONTH, 6);
	}
	
	/**
	 * 查询某星期最近的一天
	 * @param week
	 * @return
	 */
	public static Date getWeekCloseDay(int week) {
		if (week>0&&week<7) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			for (int i = 0; i < 7; i++) {
				if (week+1==calendar.get(Calendar.DAY_OF_WEEK)) {
					return calendar.getTime();
				}else {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
			}
		}
		return null;
	}
	
	/**
	 * 查询某日期星期几  0-6 
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK)-1;
	}

	/**
	 * 查询某日期星期几 0-6  
	 * @param date
	 * @return
	 */
	public static Integer getWeek(String date) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(simpdfyMd.parse(date));
			return calendar.get(Calendar.DAY_OF_WEEK)-1;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询某月第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateMonthFirstDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int lastDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, lastDay);
		return calendar.getTime();
	}

	/**
	 * 查询某月最后天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateMonthLastDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, lastDay);
		return calendar.getTime();
	}
	
	/**
	 * 查询某天距离本月最后天的长度
	 * 
	 * @param date
	 * @return
	 */
	public static int getDateMonthLastDayLength(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取时间年份
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getDateYear(Date date) {
		try {
			return Integer.valueOf(simpdfy.format(date));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	/**
	 * 获取时间年-月
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateYearMonth(Date date) {
		try {
			return simpdfyM.format(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 计算两个月份相差长度
	 * 
	 * @param sdate
	 * @param edate
	 * @return
	 */
	public static Integer getDateMonthLenth(String sStr, String eStr) {
		try {
			Date sdate = simpdfyM.parse(sStr);
			Date edate = simpdfyM.parse(eStr);
			if (sdate.getTime() <= edate.getTime()) {
				Integer index = 0;
				while (sdate.getTime() <= edate.getTime()) {
//					System.out.println(simpdfyM.format(sdate));
					index++;
					sdate = getCalendarByAdd(sdate, Calendar.MONTH, 1);
				}
				return index;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 计算两个时间天数相差长度
	 * 
	 * @param sdate
	 * @param edate
	 * @return
	 */
	public static Integer getDateDayLenth(String sStr, String eStr) {
		try {
			Date sdate = simpdfyMd.parse(sStr);
			Date edate = simpdfyMd.parse(eStr);
			if (sdate.getTime() <= edate.getTime()) {
				Integer index = 0;
				while (sdate.getTime() <= edate.getTime()) {
					index++;
					sdate = getCalendarByAdd(sdate, Calendar.DAY_OF_MONTH, 1);
				}
				return index;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 计算两个时间分钟数相差长度
	 * 
	 * @param sdate
	 * @param edate
	 * @return
	 */
	public static Integer getDateminuteLenth(String sStr, String eStr) {
		try {
			Date sdate = simpdfyMdHms.parse(sStr);
			Date edate = simpdfyMdHms.parse(eStr);
			if (sdate.getTime() <= edate.getTime()) {
				Integer index = 0;
				index=(int) ((edate.getTime()-sdate.getTime())/(1000 * 60));
				return index;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 计算两个时间小时相差长度
	 * 
	 * @param sdate
	 * @param edate
	 * @return
	 */
	public static Integer getDateHourLenth(String sStr, String eStr) {
		try {
			Date sdate = simpdfyMdHms.parse(sStr);
			Date edate = simpdfyMdHms.parse(eStr);
			if (sdate.getTime() <= edate.getTime()) {
				Integer index = 0;
				while (sdate.getTime() < edate.getTime()) {
					index++;
					sdate = getCalendarByAdd(sdate, Calendar.HOUR_OF_DAY, 1);
				}
				return index;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 分钟换小时数
	 * @param minute
	 * @return
	 */
	public static Integer getMinute2HourLenth(Integer minute) {
		Integer hourLenth=0;
		while(minute>0){
			hourLenth++;
			minute-=60;
		}
		return hourLenth;
	}

	/**
	 * 计算两个时间长度 返回字符串说明
	 * 
	 * @param data
	 * @param now
	 * @return
	 */
	public static String getDateLenthStr(Date data, Date now) {
		Long len = now.getTime() - data.getTime();
		String timeStr = null;
		if (len <= 1000 * 60) {// 1分钟内
			timeStr = "1分钟内";
		} else if (len <= 1000 * 60 * 60) {// 60分钟内
			timeStr = "1小时前";
			for (int i = 1; i < 61; i++) {
				if (len < i * 1000 * 60) {
					timeStr = (i - 1) + "分钟前";
					break;
				}
			}
		} else if (len <= 1000 * 60 * 60 * 24) {// 24小时内
			timeStr = "1天前";
			for (int i = 1; i < 25; i++) {
				if (len < i * 1000 * 60 * 60) {
					timeStr = (i - 1) + "小时前";
					break;
				}
			}
		} else if (len <= 1000 * 60 * 60 * 24 * 30L) {// 30天内
			timeStr = "1个月前";
			for (int i = 1; i < 31; i++) {
				if (len < i * 60 * 1000 * 60 * 24) {
					timeStr = (i - 1) + "天前";
					break;
				}
			}
		} else {// 超过30天
			timeStr = simpdfyMd.format(data);
		}
		return timeStr;
	}
	
	//检查两个日期是否是同一天
	public static boolean checktime(String date1 ,String date2){
		try {
			long date1long=DateUtil.simpdfyMd.parse(date1).getTime();
			long date2long=DateUtil.simpdfyMd.parse(date2).getTime();
			if (date1long==date2long) {
				return true;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}	

	/**
	 * 查询两个时间段是否有交集
	 * @param sdate1
	 * @param edate1
	 * @param sdate2
	 * @param edate2
	 * @return
	 */
	public static boolean checktimeIntersection(Date sdate1 ,Date edate1 ,Date sdate2 ,Date edate2){
		if (sdate1!=null&&edate1!=null&&sdate2!=null&&edate2!=null) {
			if ((sdate1.getTime() >= sdate2.getTime() && sdate1.getTime() <=  edate2.getTime())||
					(sdate1.getTime() <= sdate2.getTime() && edate1.getTime() >= edate2.getTime()) || 
					(edate1.getTime() >=sdate2.getTime() && edate1.getTime() <= edate2.getTime())) {
				return true;
			}		 
		}
		return false;
	}
	
	/**
	 * 查询两个时间段是否有交集
	 * @param sdate1
	 * @param edate1
	 * @param sdate2
	 * @param edate2
	 * @return
	 * @throws ParseException 
	 */
	public static boolean checktimeIntersection(String sdate1str ,String edate1str ,Date sdate2 ,Date edate2) throws ParseException{
		if (sdate1str!=null&&edate1str!=null&&sdate2!=null&&edate2!=null) {
			Date sdate1=simpdfyMdHms.parse(sdate1str); 
			Date edate1=simpdfyMdHms.parse(edate1str);
			return checktimeIntersection(sdate1, edate1, sdate2, edate2);	 
		}
		return false;
	}
	
	
	/**
	 * 获取时间戳
	 * @return
	 */
	public static Long getTimestamp(){
		return System.currentTimeMillis()/1000;
	}
	
	public static void main(String[] args) throws ParseException {

		System.out.println(getDateminuteLenth("2018-12-11 00:00:00", "2018-12-11 00:15:00"));
	}
}